package com.q.springboot.project.controller;

import com.hy.corecode.idgen.YitIdGenerator;
import com.q.springboot.framework.annotation.ResponseResult;
import com.q.springboot.framework.config.redisson.RedissonProducer;
import com.q.springboot.framework.config.redisson.RedissonProducer2;
import com.q.springboot.framework.util.*;
import com.q.springboot.project.dao.TUserDao;
import com.q.springboot.project.entity.TUser;
import com.q.springboot.project.event.LogEvent;
import com.q.springboot.project.event.SysLog;
import com.q.springboot.project.service.TUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("producer")
@AllArgsConstructor
public class ProducerController {

    @Autowired
    private RedissonProducer redissonProducer;
    @Autowired
    private RedissonProducer2 redissonProducer2;

    /**
     * 不同topic队列
     *
     * @return
     */
    @GetMapping
    public String send() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("time", "测试顺序第二条" + LocalDateTime.now());

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("name", "李白");
        map1.put("time", "无延迟" + LocalDateTime.now());
        redissonProducer2.sendMessage(map, 10, TimeUnit.SECONDS);
        redissonProducer.sendMessage(map, 30, TimeUnit.SECONDS);
        redissonProducer.sendMessage(map1);
        return "send msg";
    }

    @ResponseResult
    @GetMapping("/event")
    public String event() {
        SysLog sysLog = new SysLog();
        sysLog.setId("1").setName("log");
        SpringUtils.publishEvent(new LogEvent(sysLog));
        return "ok";
    }

    @Autowired
    private RedisLockUtil redisLockUtil;

    @GetMapping("/redisLock")
    public Object redisLock() {
        boolean b = redisLockUtil.tryLock("qmf", "a", 30, TimeUnit.SECONDS);
        if (b) {
            System.out.println("加锁成功");
        } else {
            System.out.println("失败");
        }
//        redisLockUtil.releaseLock("qmf", "a");
        return true;
    }

    @Autowired
    private FuncTest funcTest;

    @GetMapping("/redisSupplier/{id}")
    public Object redisSupplier(@PathVariable("id") String id) {
        RedisSupplier supplier = new RedisSupplier(() -> "abasdasd");
        return funcTest.getOneById(id, supplier);
    }

    @Autowired
    private MybatisBatchUtils mybatisBatchUtils;

    @GetMapping("batchInsert")
    public int ins() {
        ArrayList<TUser> tUsers = new ArrayList<>();
        for (int i = 0; i < 20000; i++) {
            TUser tUser = new TUser();
            tUser.setId(i + 1);
            tUser.setName("测试" + i + 1);
            tUser.setAge(i + 10);
            tUser.setSex("女");
            tUsers.add(tUser);
        }
        long l = System.currentTimeMillis();
        int i = mybatisBatchUtils.batchUpdateOrInsert(tUsers, TUserDao.class, (item, dao) -> dao.insert(item));
        System.out.println(System.currentTimeMillis() - l);
        return i;
    }

    @Autowired
    private TUserService tUserService;

    @GetMapping("Insert")
    public String insert() {
//        for (int i = 0; i < 20000; i++) {
        TUser tUser = new TUser();
        tUser.setId(Integer.parseInt(String.valueOf(System.currentTimeMillis())));
        tUser.setName("测试");
        tUser.setAge(10);
        tUser.setSex("女");
        tUserService.insert(tUser);
//        }
        return "ok";
    }

    @GetMapping("pluginQuery")
    public TUser pluginQuery() {
        return tUserService.queryById(1);
    }

    @GetMapping("ttest")
    public TUser ttest() {
        return tUserService.tselect("'' or 1=1 limit 1");
    }

    @Autowired
    YitIdGenerator yitIdGenerator;

    @GetMapping(value = "/snowId")
    public String snowId() {
        long next = yitIdGenerator.next();
        return next + "";
    }


}
